package cn.edu.hziee;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class CartServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        List<Production> productions = null;
        HttpSession session = request.getSession(true);
        int price =0;
        if(session == null){
            out.write("对不起！您还没有购买任何商品！");
        }else{
            productions = (List<Production>) session.getAttribute("cart");
            if (productions == null){
                out.write("对不起！您还没有购买任何商品！");
            }else {
                out.println("购物车：<br />");
                for (int i = 0; i < productions.size(); i++) {
                    Production production = productions.get(i);
                    out.println("<a href='/ShoppingCart/DeleteServlet?id="+i+"'>X</a>  " + production.getName() + "  " + production.getPrice() + "元  数量：  <a href='/ShoppingCart/ReduceServlet?id="+i+"'>-</a> " + production.getNumber() + " <a href='/ShoppingCart/AddServlet?id="+i+"'>+</a> 个<br/>");
                    price = price +production.getNumber()*production.getPrice();
                }
            }
        }
        out.println("总计： "+price+"元");
    }
}
